

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @Description
 * @Author 薛铁琪
 * @CreateTime 2021/4/18 14:24
 * @Version 1.0
 */
public class Graph extends JPanel {

    //overall situation
    public static double minLatitude = Integer.MAX_VALUE;
    public static double minLongitude = Integer.MAX_VALUE;
    public static double maxLatitude = Integer.MIN_VALUE;
    public static double maxLongitude = Integer.MIN_VALUE;
    public static double xWin, yWin;
    public static ArrayList<Road> allRoads = new ArrayList<>();
    public static HashMap<String, Intersection> allIntersectionMap = new HashMap<>();
    public static boolean isShow;
    //mini tree
    public static boolean isMinTree;
    public static ArrayList<Road> minTress = new ArrayList<>();
    //min path
    public static boolean isDijkstras;
    public static String begin;
    public static String end;
    public static ArrayList<Road> dijkstras = new ArrayList<>();



    @Override
    public void paintComponent(Graphics page) {
        Graphics2D graphics2D = (Graphics2D) page;
        super.paintComponent(graphics2D);
        xWin = this.getWidth() / (maxLongitude - minLongitude);
        yWin = this.getHeight() / (maxLatitude - minLatitude);
        doGraph(allRoads, graphics2D, Color.BLACK, false);
        if (isMinTree) {
            doGraph(minTress, graphics2D, Color.RED, true);
        }
        if (isDijkstras) {
            doGraph(dijkstras, graphics2D, Color.BLUE, true);
        }
    }

    private void doGraph(ArrayList<Road> roads, Graphics2D graphics2D, Color color, boolean isBold) {
        graphics2D.setColor(color);
        if (isBold) {
            graphics2D.setStroke(new BasicStroke(3));
        }
        Intersection intersection1;
        Intersection intersection2;
        for (Road r : roads) {
            intersection1 = allIntersectionMap.get(r.intersect1);
            intersection2 = allIntersectionMap.get(r.intersect2);
            double x1 = intersection1.longitude;
            double y1 = intersection1.latitude;
            double x2 = intersection2.longitude;
            double y2 = intersection2.latitude;
            graphics2D.draw(new Line2D.Double((x1 - minLongitude) * xWin, getHeight() - ((y1 - minLatitude) * yWin),
                    (x2 - minLongitude) * xWin, getHeight() - ((y2 - minLatitude) * yWin)));

        }
    }

}
